package com.carlife.ui.widget;

import java.math.BigDecimal;

import com.carlife.util.Constants;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DigitalBar extends RelativeLayout {
	
	private Button minusBotton = null;
	private Button plusBotton = null;
	private EditText valueText = null;
	private TextView unitText = null;
	
	private BigDecimal value = new BigDecimal(0);
	private BigDecimal interval = new BigDecimal(1);
	private String unitStr = "";
	private BarValueChangedListener valueChangeListener = null;

	public DigitalBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public DigitalBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public DigitalBar(Context context) {
		super(context);
		init(context);
	}
	
	private void init(Context ctx) {
		minusBotton = new Button(ctx);
		minusBotton.setId(1);
		RelativeLayout.LayoutParams minusLayout = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT, 
				ViewGroup.LayoutParams.WRAP_CONTENT);		
		minusLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		minusLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		minusBotton.setText("-");
		minusBotton.setTextColor(Color.BLUE);
		minusBotton.setTextSize(TypedValue.COMPLEX_UNIT_PX,25);
		this.addView(minusBotton,minusLayout);
		minusBotton.setOnClickListener(new DigitalBarClickListener(this, interval.negate()));
		
		valueText = new EditText(ctx);
		valueText.setId(2);
		valueText.setTextColor(Color.RED);
		RelativeLayout.LayoutParams valueLayout = new RelativeLayout.LayoutParams(
				120, 
				ViewGroup.LayoutParams.WRAP_CONTENT);
		valueLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		valueLayout.addRule(RelativeLayout.RIGHT_OF, 1);
		this.addView(valueText,valueLayout);		
		valueText.setOnFocusChangeListener(new DigitalBarFoucusChangeListener(this));
		
		plusBotton = new Button(ctx);
		plusBotton.setId(3);
		RelativeLayout.LayoutParams plusLayout = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT, 
				ViewGroup.LayoutParams.WRAP_CONTENT);
		plusLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		plusLayout.addRule(RelativeLayout.RIGHT_OF, 2);
		plusBotton.setText("+");
		plusBotton.setTextColor(Color.BLUE);
		plusBotton.setTextSize(TypedValue.COMPLEX_UNIT_PX,25);
		this.addView(plusBotton,plusLayout);
		plusBotton.setOnClickListener(new DigitalBarClickListener(this, interval));
		
		unitText = new TextView(ctx);
		unitText.setId(4);
		RelativeLayout.LayoutParams unitLayout = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT, 
				ViewGroup.LayoutParams.WRAP_CONTENT);
//		unitLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		unitLayout.addRule(RelativeLayout.CENTER_VERTICAL);
		unitLayout.addRule(RelativeLayout.RIGHT_OF, 3);
		unitText.setText(unitStr);
		unitText.setTextColor(Color.BLUE);
		unitText.setTextSize(TypedValue.COMPLEX_UNIT_PX,25);
		this.addView(unitText,unitLayout);
	}
	
	public void setValue(BigDecimal value) {
		this.value = value;
		this.valueText.setText(this.value.toPlainString());
	}
	
	public BigDecimal getValue() {		
		return this.value;
	}
	
	public void setInterval(BigDecimal interval){
		this.interval = interval;
	}
	
	public void setUnitString(String unitStr) {
		this.unitStr = unitStr;
		this.unitText.setText(unitStr);
	}
	
	public void setOnValueChangedListener(BarValueChangedListener valueChangeListener) {
		this.valueChangeListener = valueChangeListener;
	}
	
	private void changeValue(BigDecimal intervalValue) {
		Log.d(Constants.LOG_TAG,"Before value = " + this.value.toPlainString() + ", interval=" + intervalValue.toPlainString());
		int scale = this.value.scale();
		this.value = this.value.add(intervalValue);
		this.value.setScale(scale);
		
		Log.d(Constants.LOG_TAG,"After value = " + this.value.toString());
		this.valueText.setText(this.value.toPlainString());
		
		if (null != valueChangeListener) {
			valueChangeListener.onChanged(this);
		}
	}
	
	class DigitalBarClickListener implements OnClickListener {
		private DigitalBar bar = null;
		private BigDecimal tmpInterval = null;

		public DigitalBarClickListener(DigitalBar bar, BigDecimal interval) {
			super();
			this.bar = bar;
			this.tmpInterval = interval;
		}

		@Override
		public void onClick(View arg0) {
			bar.changeValue(tmpInterval);
		}
		
	}
	
	class DigitalBarFoucusChangeListener implements OnFocusChangeListener {
		private DigitalBar originalBar = null;
		
		public DigitalBarFoucusChangeListener(DigitalBar originalBar) {
			this.originalBar = originalBar;
		}

		@Override
		public void onFocusChange(View arg0, boolean arg1) {
			String newValueStr = originalBar.valueText.getText().toString();
			originalBar.setValue(new BigDecimal(newValueStr));
			
			if (null != valueChangeListener) {
				valueChangeListener.onChanged(originalBar);
			}				
		}
	}
	
}
